package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.StringTools;
import com.example.demo.common.UserSessionTools;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.vo.ArticleInfoVO;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest request) {
        //1.非空验证
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent()))
            return AjaxResult.fail(-1, "参数异常!");
        //2.得到uid 组装数据
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        //3.持久化数据
        int result = articleService.add(articleInfo);
        return AjaxResult.success(result);
    }

    @RequestMapping("/getdetailbyid")
    public AjaxResult getDetailByIdAndUid(Integer id, HttpServletRequest request) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "参数异常");
        }
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        return AjaxResult.success(articleService.getDetailByIdAndUid(id, userInfo.getId()));
    }

    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo, HttpServletRequest request) {
        //1.非空验证
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())
                || articleInfo.getId() <= 0)
            return AjaxResult.fail(-1, "参数异常!");
        //2.得到uid
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        int result = articleService.update(articleInfo);
        return AjaxResult.success(result);
    }
    @RequestMapping("/getdetail")
    public AjaxResult getDetail(Integer id){
        if (id == null || id <=0 ){
            return AjaxResult.fail(-1,"参数有误");
        }
        ArticleInfoVO articleInfoVO = articleService.getDetail(id);
        return AjaxResult.success(articleInfoVO);
    }
    @RequestMapping("/addrcount")
    public AjaxResult addRcount(Integer id){
        if (id == null || id <= 0) return AjaxResult.fail(-1,"参数有误");
        return AjaxResult.success(articleService.addRcount(id));
    }
    @RequestMapping("/mylist")
    public AjaxResult MyList(HttpServletRequest request) {
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
       List<ArticleInfo> list = articleService.getListByUid(userInfo.getId());
       //截取文章摘要
       for (ArticleInfo art : list) {
           art.setContent(StringTools.subLength(art.getContent(),100));
        }
       return AjaxResult.success(list);
    }
    @RequestMapping("/del")
    public AjaxResult del(Integer id,HttpServletRequest request){
        if (id == null || id <= 0){
            return AjaxResult.fail(-1,"参数有误");
        }
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        return AjaxResult.success(articleService.del(id,userInfo.getId()));
    }
    @RequestMapping("/getlistbypage")
    public AjaxResult getListByPage(Integer pageSize,Integer pageIndex){
       //做默认值处理
        if (pageSize ==null || pageSize == 0){
            pageSize = 2;
        }
        if (pageIndex == null || pageIndex <= 0){
            pageIndex = 1;
        }
        //分页公式
        int offset = (pageIndex -1 )*pageSize;
        List<ArticleInfo> list = articleService.getListByPage(pageSize,offset);
        //截取文章摘要
        for (ArticleInfo art : list) {
            art.setContent(StringTools.subLength(art.getContent(),100));
        }
        return AjaxResult.success(list);
    }
    @RequestMapping("/getcount")
    public AjaxResult getCount(){
        return AjaxResult.success(articleService.getCount());
    }
     /*
        修改文章后保存文章
     */
    @RequestMapping("/editsave")
    public AjaxResult editSave(ArticleInfo articleInfo,HttpServletRequest request){
        //1.非空验证
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())
                || articleInfo.getId() <= 0)
            return AjaxResult.fail(-1, "参数异常!");
        //2.得到uid
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        //把当前文章标记为 草稿
        articleInfo.setState(-1);
        int result = articleService.editSave(articleInfo);
        return AjaxResult.success(result);
    }

    @RequestMapping("/mydraftlist")
    public  AjaxResult myDraftList(HttpServletRequest request) {
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        List<ArticleInfo> list = articleService.getDaftListByUid(userInfo.getId());
        //截取文章摘要
        for (ArticleInfo art : list) {
            art.setContent(StringTools.subLength(art.getContent(),100));
        }
        return AjaxResult.success(list);
    }

    /*
        修改文章成定时发布文章
        todo:spring解析不了时间
     */
    @RequestMapping("/timeoutupdate")
    public  AjaxResult timeoutUpdate(String publishTimeStr,ArticleInfo articleInfo ,HttpServletRequest request) throws ParseException {
        //1.非空验证
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())
                || articleInfo.getId() <= 0)
            return AjaxResult.fail(-1, "参数异常!");
        //2.得到uid
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        //格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(publishTimeStr, formatter);
//        System.out.println(localDateTime+"==============");
        //设置定时时间
        articleInfo.setPublishtime(localDateTime);
        int result = articleService.timeoutUpdate(articleInfo);
        return AjaxResult.success(result);
    }
    @RequestMapping("/addtimeout")
    public AjaxResult addtimeout(String publishTimeStr,ArticleInfo articleInfo, HttpServletRequest request) {
        //1.非空验证
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent()))
            return AjaxResult.fail(-1, "参数异常!");
        //2.得到uid 组装数据
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        //格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(publishTimeStr, formatter);
        //设置定时时间
        articleInfo.setPublishtime(localDateTime);
        //3.持久化数据
        int result = articleService.addtimeout(articleInfo);
        return AjaxResult.success(result);
    }
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse("2023/12/12 12:00:00", formatter);
        System.out.println(localDateTime+"==============");
    }
}
